var _d_url = window.parent._index_path;

//初始化方法
function initDSColumn(){
	var dsId = "ds_sys_datastore1";
	$.ajax({
		type:'post',
		url:_d_url+'framework/loadColumnsU',
		data:{'colId':dsId,'type':'5'},
		success:function(data){
			if(data != null){
				producePage(data[0],"dsDivStr",'5','');
			}
			//应该有三个初始化datagrid的方法
			loadColumnQr();  //查询界面
			loadColumnAdd(); //添加界面
			loadColumnConfig(); //多条件界面 
			loadColumnInfo(); //基本信息
		}
	})
}
//配置说明
 //将editor:之后的东西换成要配置的
//1.目前datagrid的editor都配置为text，如果为datalist或者是select这些情况，则根据原来配置说明进行配置
//2.如果要配置翻译说明则在editor后面加逗号然后是formatter:eval('window.parent.dz_bumenfmat') dz_bumenfmat这个就是数据库中format_count的值

//初始化查询界面方法
function loadColumnQr(){
	var lastIndex;
	$('#colnQrList').datagrid({
		iconCls:'icon-save',
		height:500 ,
		width:document.documentElement.clientWidth - 20,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		columns:[[
		  {field:"DS_ID",title:'DS_ID',hidden:true},
          {field:"NAME",title:'名称',width:'100'},
          {field:"ORDERNUM",title:'序列号',width:'100',editor:{type:'text'}},
          {field:"COMMON_USE",title:'是否是查询条件',width:'100',editor:{type:'text'}}, 
          {field:"EDITYPE",title:'列表弹框对照',width:'100',editor:{type:'text'}},
          {field:"INPUTTYPE",title:'显示类型',width:'100',editor:{type:'text'}},
          {field:"ISSHOW",title:'是否显示',width:'100',editor:{type:'text'}},
          {field:"HIDDEN",title:'是否隐藏',width:'100',editor:{type:'text'}},
          {field:"ROW_STYLER",title:'行样式',width:'100',editor:{type:'text'}},
          {field:"WIDTH",title:'列宽',width:'100'}
          ]],
		singleSelect:true,
		onClickRow:function(rowIndex,rowData){
			$('#colnQrList').datagrid('endEdit', lastIndex);
			$('#colnQrList').datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		}
	});   
}
//初始化添加界面方法
function loadColumnAdd(){
	var lastIndex;
	$('#colnAddList').datagrid({
		iconCls:'icon-save',
		height:500 ,
		width:document.documentElement.clientWidth - 20,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		columns:[[
		  {field:"DS_ID",title:'DS_ID',hidden:true},
          {field:"NAME",title:'名称',width:'100'},
          {field:"ORDER_NUM_ADD",title:'序列号',width:'100',editor:{type:'text'}},
          {field:"AUTO_ADD",title:'是否自动填充',width:'100',editor:{type:'text'}}, 
          {field:"DEFAULT_VALUE_ADD",title:'默认值',width:'100',editor:{type:'text'}},
          {field:"EDITYPE_ADD",title:'对照弹框类型',width:'100',editor:{type:'datalist',options: {editype:'dz_all_dz',dsid:'ds_sys_columns2'}}},
          {field:"HEIGHT_ADD",title:'高度',width:'100',editor:{type:'text'}},
          {field:"HIDDEN_ADD",title:'是否隐藏',width:'100',editor:{type:'text'}},
          {field:"IS_PARENT_GROUP_ADD",title:'分组',width:'100',editor:{type:'text'}},
          {field:"IS_SHOW_ADD",title:'是否显示',width:'100',editor:{type:'text'}}, 
          {field:"READONLY_ADD",title:'是否只读',width:'100',editor:{type:'text'}},
          {field:"REQUIRED_ADD",title:'是否必录',width:'100',editor:{type:'text'}},
          {field:"IS_COPY",title:'是否可复制',width:'100',editor:{type:'text'}},
          {field:"NEW_LINE_ADD",title:'另起一行',width:'100',editor:{type:'text'}},
          {field:"SINGLE_ROW_ADD",title:'独立一行',width:'100',editor:{type:'text'}},
          {field:"WIDTH_ADD",title:'列宽',width:'100',editor:{type:'text'}},
          {field:"COLS_COUNT_ADD",title:'界面所占的列数',width:'100'}
          ]],
		singleSelect:true,
		onClickRow:function(rowIndex,rowData){
			$('#colnAddList').datagrid('endEdit', lastIndex);
			$('#colnAddList').datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		}
	});   
}
//初始化多条件界面方法
function loadColumnConfig(){
	var lastIndex;
	$('#colnConfigList').datagrid({
		iconCls:'icon-save',
		height:500 ,
		width:document.documentElement.clientWidth - 20,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		columns:[[]],
		singleSelect:true,
		columns:[[
		  {field:"DS_ID",title:'DS_ID',hidden:true},
          {field:"NAME",title:'名称',width:'100'},
          {field:"ORDER_NUM_QR",title:'序列号',width:'100', editor:{type:'numberbox'}},
          {field:"DEFAULT_VALUE_QR",title:'默认值',width:'100',editor:{type:'text'}},
          {field:"INPUTTYPE_QR",title:'对照弹框类型',width:'100',editor:{type:'text'}},
          {field:"HIDDEN_QR",title:'是否隐藏',width:'100',editor:{type:'text'}},
          {field:"IS_PARENT_GROUP_QR",title:'分组',width:'100',editor:{type:'text'}},
          {field:"IS_SHOW_QR",title:'是否显示',width:'100',editor:{type:'text'}},
          {field:"NEW_LINE_QR",title:'另起一行',width:'100',editor:{type:'text'}},
          {field:"SINGLE_ROW_QR",title:'独立一行',width:'100',editor:{type:'text'}}
          ]],
		singleSelect:true,
		onClickRow:function(rowIndex,rowData){
			$('#colnConfigList').datagrid('endEdit', lastIndex);
			$('#colnConfigList').datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		}
	});   
}
//通用信息
function loadColumnInfo(){
	var lastIndex;
	$('#colnInfoList').datagrid({
		iconCls:'icon-save',
		height:500 ,
		width:document.documentElement.clientWidth - 20,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		pageList:[30,50,100,150],
		columns:[[]],
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		columns:[[
	      {field:"DS_ID",title:'DS_ID',hidden:true},
          {field:"NAME",title:'名称',width:'100'},
          {field:"COLN_NAME",title:'显示名称',width:'100',editor:{type:'text'}},
          {field:"JAVA_PROPERTY_RECORDED",title:'数据库列名',width:'100',editor:{type:'text'}},
          {field:"MODEL_TYPE",title:'弹窗类型',width:'100',editor:{type:'text'}},
          {field:"IS_MULTI_CLNS",title:'是否是一对多弹窗',width:'100',editor:{type:'text'}},
          {field:"MULTI_RETURN_FUNC",title:'一对多弹窗的回调',width:'100',editor:{type:'text'}},
          {field:"DATALISTFUNC",title:'普通弹窗的回调',width:'100',editor:{type:'text'}},
          {field:"SINGLE_MORE",title:'弹窗的单选/多选',width:'100',editor:{type:'text'}},
          {field:"DATALIST_FILTER",title:'弹窗的过滤条件',width:'100',editor:{type:'text'}},
          {field:"FORMAT_CONT",title:'对照信息编码类型转换',width:'100',editor:{type:'text'}},
          {field:"IS_FK",title:'是否是主键',width:'100',editor:{type:'text'}},
          {field:"RELATION_DSID",title:'关联的dsId',width:'100',editor:{type:'text'}},
          {field:"RELATION_FLAG",title:'关联标志位',width:'100',editor:{type:'text'}},
          {field:"RELATION_FUNC",title:'关联调用的方法',width:'100',editor:{type:'text'}},
          {field:"EDITOR",title:'明细编辑',width:'100',editor:{type:'text'}},
          {field:"FROZEN",title:'列表固定列',width:'100',editor:{type:'text'}},
          {field:"IS_SHOW_QR",title:'是否显示',width:'100',editor:{type:'text'}},
          {field:"SINGLE_ROW_QR",title:'独立一行',width:'100',editor:{type:'text'}}
          ]],
		singleSelect:true,
		onClickRow:function(rowIndex,rowData){
			$('#colnInfoList').datagrid('endEdit', lastIndex);
			$('#colnInfoList').datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		}
	});   
}
//生成字段的方法
function newColns(){
	var dsId = $('#dsDivStr input[qucln=DS_ID]').val();
	if(dsId == "" || dsId == null){
		$.messager.alert('提示','请先保存SYS_DATASTORE!');
	}else{
		$.ajax({
			type:'post',
			url:_d_url+'store/getStore',
			data:{'dsId':dsId},
			success:function(data){
				if(data == null){
					$.messager.alert("提示","DS_ID不存在，请先保存SYS_DATASTORE!");
				}else{
					var sqlText = $('#dsDivStr textarea[qucln=SQL_TEXT]').val();
					if(sqlText != "" || sqlText != null){
						$.ajax({
							type:'post',
							url:_d_url+'store/newColumns',
							data:{'sqlText':sqlText,'dsId':dsId },
							success:function(data){
								$('#colnQrList').datagrid('loadData',data.rows);
								$('#colnAddList').datagrid('loadData',data.rows);
								$('#colnConfigList').datagrid('loadData',data.rows);
								$('#colnInfoList').datagrid('loadData',data.rows);
							}
						})
					}else{
						$.messager.alert('提示','sql没有获取到！');
					}
				}
			}
		})
	}
}
//dsId 文本框查找dataStore的方法
function dsInfo(){
	var dsId = $('#dsIdVal').val();
	$.ajax({
		type:'post',
		url:_d_url+'framework/dsInfoListColn',
		data:{'dsId':dsId},
		success:function(data){
			var dataRow= data[0][0];
			var colnRow= data[1];
			fillInfo('dsDivStr',dataRow);
			$('#colnQrList').datagrid('loadData',colnRow);
			$('#colnAddList').datagrid('loadData',colnRow);
			$('#colnConfigList').datagrid('loadData',colnRow);
			$('#colnInfoList').datagrid('loadData',colnRow);
			$('#dsSaveButn').attr('sou','2');
		}
	})
}
//保存dataList方法
function saveCommInfo(dgName){
	var index = 0;
	var mapRecords = new Array();
	var receiveMobj = new Array();
	var dsId = $('#dsDivStr input[qucln=DS_ID]').val();
	var allColn = $("#"+dgName+"").datagrid('getRows');
	for(var i=0;i<allColn.length;i++){
		$('#'+dgName+'').datagrid('endEdit',i);
	}
	var updateRows=  $("#"+dgName+"").datagrid('getChanges','updated');
	console.log(updateRows);
	var colnNameList = new Array();
	if(updateRows.length<=0){
		$.messager.alert('信息提示',"您没有对数据进行修改还不能进行保存");
		return;
	}
	//得到明细表中跟新的数据
	if(updateRows.length>0){
		for(var i = 0; i <updateRows.length ; i++){
			colnNameList[i]= updateRows[i].NAME;
			var tmp = {};
			for( p in updateRows[i]){
				tmp[p] = updateRows[i][p];
			}
			var row={"dsId":dsId,'mapRecord':tmp};
			mapRecords[index] = row;
			index++;
		}
	}
	console.log(mapRecords);
	receiveMobj={'colnNameList':colnNameList,"mapRecords":mapRecords};
	$.ajax({
		url:_m_url+'store/saveConfigPage',
		type:'post',
		contentType: "application/json;charset=utf-8",
		dataType: "json",
		async:false,
		data:JSON.stringify(receiveMobj),
		success:function(data){
			if(data.msg=='success'){
				$.messager.alert("提示","保存成功！");
			}else{
				$.messager.alert("提示","保存失败！");
			}
			
		}
	})
}

